package kr.ac.hufs;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;

public class FileSystemTreeModel implements TreeModel,Serializable {
	protected FileFolder root;
	protected Vector listeners;
	protected boolean directoryOnly;
	
	public FileSystemTreeModel(FileFolder r,boolean dir) {
		root = r;
		directoryOnly = dir;
		listeners = new Vector();
	}
	
	public Object getRoot() {
		return root;
	}
	
	public Object getChild(Object parent, int index) {
		Object retVal = null;
		Vector children;
		
		if(parent instanceof FileFolder){
			if(directoryOnly){
				children = ((FileFolder)parent).getSubdirectory();
			}else{
				children = ((FileFolder)parent).getChildern();
			}
			if(children != null){
				if(index <children.size()){
					retVal = children.elementAt(index);
				}
			}
		}
		return retVal;
	}
	
	public int getChildCount(Object parent) {
		int retVal = 0;
		Vector children;
		
		if(parent instanceof FileFolder){
			if(directoryOnly){
				retVal = ((FileFolder)parent).getSubdirectoryCount();
			}else{
				retVal = ((FileFolder)parent).getChildCount();
			}
		}
		return retVal;
	}
	
	public boolean isLeaf(Object node) {
		boolean retVal = true;
		File file;
		
		if(node instanceof FileFolder){
			file = ((FileFolder)node).getFile();
			retVal = file.isFile();
		}
		return retVal;
	}
	
	public void  valueForPathChanged(TreePath path, Object newVal) {
		FileFolder holder;
		File file;
		File newFile;
		
		try{
			holder = (FileFolder)path.getLastPathComponent();
			file = holder.getFile();
			newFile = new File(file.getParent(),newVal.toString());
			
			if(file.renameTo(newFile)){
				Object objPath[];
				Object pobjPath[];
				TreePath parentPath;
				FileFolder parentHolder;
				Vector files;
				int i,max;
				int childInd[];
				Object child[];
				
				holder.setFile(newFile);
				objPath = path.getPath();
				
				max = objPath.length-1;
				pobjPath = new Object[max];
				
				for(i=0; i<max ; i++){
					pobjPath[i] = objPath[i];
				}
				
				parentPath = new TreePath(pobjPath);
				parentHolder =(FileFolder)parentPath.getLastPathComponent();
				files = parentHolder.getChildern();
				childInd = new int[1];
				childInd[0] = files.indexOf(holder);
				child = new Object[1];
				child[0] = holder;
				fireValueChanged(parentPath, childInd, child);				
			}
		}catch(Exception exp){}
	}
	
	public int getIndexOfChild(Object parent, Object child){
		int retVal = -1;
		Vector children;
		
		if(parent instanceof FileFolder){
			if(directoryOnly){
				children = ((FileFolder)parent).getSubdirectory();
			}else{
				children = ((FileFolder)parent).getChildern();
			}
			if(children != null){
				retVal = children.indexOf(child);
			}
		}
		return retVal;
	}
	
	public void addTreeModelListener(TreeModelListener l){
		if(l != null && !listeners.contains(l))
			listeners.addElement(l);
	}
	
	public void removeTreeModelListener(TreeModelListener l){
		listeners.removeElement(l);
	}
	
	protected void fireValueChanged(TreePath path,int[] ind, Object[] childern) {
		TreeModelEvent evt;
		int i,max;
		TreeModelListener curL;
		
		evt = new TreeModelEvent(this, path, ind, childern);
		max = listeners.size();
		for(i = 0; i<max; i++){
			curL = (TreeModelListener)listeners.elementAt(i);
			curL.treeNodesChanged(evt);
		}
	}
}
